=over

=item getsockopt SOCKET,LEVEL,OPTNAME
X<getsockopt>

Queries the option named OPTNAME associated with SOCKET at a given LEVEL.
Options may exist at multiple protocol levels depending on the socket
type, but at least the uppermost socket level SOL_SOCKET (defined in the
L<C<Socket>|Socket> module) will exist.  To query options at another
level the protocol number of the appropriate protocol controlling the
option should be supplied.  For example, to indicate that an option is
to be interpreted by the TCP protocol, LEVEL should be set to the
protocol number of TCP, which you can get using
L<C<getprotobyname>|/getprotobyname NAME>.

The function returns a packed string representing the requested socket
option, or L<C<undef>|/undef EXPR> on error, with the reason for the
error placed in L<C<$!>|perlvar/$!>.  Just what is in the packed string
depends on LEVEL and OPTNAME; consult L<getsockopt(2)> for details.  A
common case is that the option is an integer, in which case the result
is a packed integer, which you can decode using
L<C<unpack>|/unpack TEMPLATE,EXPR> with the C<i> (or C<I>) format.

Here's an example to test whether Nagle's algorithm is enabled on a socket:

    use Socket qw(:all);

    defined(my $tcp = getprotobyname("tcp"))
        or die "Could not determine the protocol number for tcp";
    # my $tcp = IPPROTO_TCP; # Alternative
    my $packed = getsockopt($socket, $tcp, TCP_NODELAY)
        or die "getsockopt TCP_NODELAY: $!";
    my $nodelay = unpack("I", $packed);
    print "Nagle's algorithm is turned ",
           $nodelay ? "off\n" : "on\n";

Portability issues: L<perlport/getsockopt>.

=back